import { ref, onMounted, onUnmounted } from '@vue/runtime-core'

export const useKeyboard = (map) => {
    const handleKeydown = (e) => {
        const cb = map[e.code];
        if (cb && cb.keydown) {
            cb.keydown(e)
        }
    }
    const handleKeyup = (e) => {
        const cb = map[e.code];
        if (cb && cb.keyup) {
            cb.keyup(e)
        }
    }
    onMounted(() => {
        window.addEventListener('keydown', handleKeydown)
        window.addEventListener('keyup', handleKeyup)
    })
    onUnmounted(() => {
        window.removeEventListener('keydown', handleKeydown)
        window.removeEventListener('keyup', handleKeyup)
    })
}